﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using borkoSi.Models;
using borkoSi.ViewModels;
using Article = borkoSi.ViewModels.Article;
using borkoSi.Addons.Classes;

namespace borkoSi.Controllers
{
    public class HomeController : Controller
    {

        private borkoSiEntities db = new borkoSiEntities();

        public ActionResult Index()
        {
            HomeViewModel home = new HomeViewModel();

            // random seed - used for random proverb and random gallery set
            var rnd = new Random();

            // BIRTHDAYS
            // read user data
            List<UserBirthday> userBirthday = db.Birthdays.Select(r => new UserBirthday()
            {
                Name = r.Name,
                Date = r.Date,
                Day = r.Date.Day,
                Month = r.Date.Month,
                Hour = r.Date.Hour,
                Minute = r.Date.Minute,
                Second = r.Date.Second
            }).OrderBy(r => r.Month).Take(4).ToList();

            for(int i = 0; i < userBirthday.Count; i++)
            {
                userBirthday[i].Year = getNextBirthdayYear(userBirthday[i].Date);
            }

            home.Birthdays = userBirthday;

            // PROVERBS
            List<int> proverbIds = db.Proverbs.Select(r => r.ProverbId).ToList();
            int id = proverbIds[rnd.Next(1, proverbIds.Count)];

            home.ProverbAuthor = db.Proverbs.Find(id).Author.ToString();
            home.ProverbContent = db.Proverbs.Find(id).Content.ToString();
            home.ProverbLink = db.Proverbs.Find(id).Link != null ? db.Proverbs.Find(id).Link.ToString() : "";
            
            // GALLERY
            int randomGallerySet = rnd.Next(1, 4);
            home.GallerySetNumber = db.Galleries.Find(randomGallerySet).GalleryNumber.ToString();
            home.GalleryId = db.Galleries.Find(randomGallerySet).GalleryId;

            // ARTICLES
            List<Article> articles = db.Articles.Select(r => new Article()
            {
                NewsId = r.NewsId,
                Title = r.Title,
                Content = r.Content,
                GalleryId = r.GalleryId,
                DateCreated = r.DateCreated
            }
            ).OrderByDescending(r => r.DateCreated).ToList();

            foreach (Article a in articles)
            {
                a.NewsIdRoman = RomanNumerals.ToString(a.NewsId);
            }

            home.Articles = articles;

            // version number
            ViewBag.VersionNumber = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            return View(home);
        }

        //public ActionResult About()
        //{
        //    ViewBag.Message = "Your app description page.";

        //    return View();
        //}   

        //public ActionResult Contact()
        //{
        //    ViewBag.Message = "Your contact page.";

        //    return View();
        //}

        /*
         * return year of next birthday
         */
        public int getNextBirthdayYear(DateTime dt)
        {
            var currYear = DateTime.Now.Year;

            // set birthday year to current year
            DateTime currYearBirthday = new DateTime(currYear, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);
            DateTime now = DateTime.Now;

            // compare if this years birthdays has already occured
            // if true -> next birthday is next year
            // if false -> next birthday is this year
            int nextBirthdayYear = DateTime.Compare(now, currYearBirthday) > 0 ? nextBirthdayYear = currYear + 1 : nextBirthdayYear = currYear;

            return nextBirthdayYear;
        }
    }
}
